<template>
	<u-form :model="form" ref="uForm" style="padding: 15rpx;">
		<u-form-item label="旧密码" prop="oldPassword" label-width="150" label-align="left">
			<u-input type="password" v-model="form.oldPassword" placeholder="请输入旧密码" :password-icon="true"/>
		</u-form-item>
		<u-form-item label="新密码" prop="newPassword" label-width="150" label-align="left" :border-bottom="false">
			<u-input type="password" v-model="form.newPassword" placeholder="请输入新密码" :password-icon="true"/>
		</u-form-item>
		<u-form-item label="确认密码" prop="twoNewPassword" label-width="150" label-align="left" :border-bottom="false">
			<u-input type="password" v-model="form.twoNewPassword" placeholder="请确认密码" :password-icon="true"/>
		</u-form-item>
	</u-form>
	<view class="sureBtn" @click="sureBtn">确定</view>
</template>

<script>
	import { validatorPw } from '@/pages/user/validate.js'
	import { userStore } from '@/stores/userInfo/userInfo.js'
	import { user } from '@/api'
	import { removeTokenExpired, removeToken } from '@/utils/localStoreToken.js'
	export default{
		data(){
			return {
				form: {
					oldPassword: '',
					newPassword: '',
					twoNewPassword: '',
					uid: ''
				},
				twoNewPassword: '',
				flag: true,
				rules: {
					oldPassword: [
						{
							required: true,
							validator: validatorPw,
							trigger: ['blur', 'change'],
						}
					],
					newPassword: [
						{
							required: true,
							validator: validatorPw,
							trigger: ['blur', 'change']
						}
					],
					twoNewPassword: [
						{
							required: true,
							validator: (ruls,value,callback) => {
								if(value.trim() === '') {
									callback('密码不能为空')
									return false
								}
								if(value.length < 6 || value.length > 12) {
									callback('请输入6-12的密码')
									return false
								}
								if(value !== this.form.newPassword){
									callback('两次密码不一致')
									return false
								}
								return true
							},
							trigger: ['blur', 'change']
						}
					]
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
	
		methods:{
			sureBtn() {
				if(this.flag) {
					this.flag = false
					let user = userStore()
					this.form.uid = user.userInfo._id
					this.modifyPassword()
				}
			},
			modifyPassword() {
				this.$refs.uForm.validate((valid) => {
					if(valid){
						this.twoNewPassword = this.form.twoNewPassword
						// 整理参数
						delete this.form.twoNewPassword
						user.reqUpdatePwd(this.form).then(res => {
							if(res.code === 0 ) {
								let user = userStore()
								uni.$u.toast('修改成功，重新登录')
								// 清除token 和时间
								removeToken()
								removeTokenExpired()
								// 清空userInfo
								// user.clearUserInfo()
								setTimeout(() => {
									this.flag = true
									uni.redirectTo({
										url: '/pages/user/login/login'
									})
								},500)
								
							}else{
								uni.$u.toast(res.errMsg)
								this.form.twoNewPassword= this.twoNewPassword
							}
						}).catch(err => {
							console.log(err)
						})
					}
				})
			}
		}
	}
	
</script>

<style lang="scss">
.sureBtn{
	text-align: center;
	line-height: 100rpx;
	background-color: skyblue;
	width: 500rpx;
	margin: 0 auto;
	border-radius: 5rpx;
	color: #fff;
	&:active{
		opacity: 0.5;
	}
}
</style>
